/**
 * @file clock_ui.c
 * @brief Enhanced Clock UI implementation that perfectly matches the reference image
 */

#include "lvgl/lvgl.h"
#include <time.h>
#include <stdio.h>

/* Enhanced color definitions matching the reference image exactly */
#define COLOR_BG_TOP       lv_color_hex(0x5CB3E0)     /* Top gradient blue */
#define COLOR_BG_MID1      lv_color_hex(0x4FADDB)     /* Header background */
#define COLOR_BG_MID2      lv_color_hex(0x87CEEB)     /* Middle gradient blue */
#define COLOR_BG_BOTTOM    lv_color_hex(0x2E86AB)     /* Bottom status blue */
#define COLOR_TIME_RED     lv_color_hex(0xFF4757)     /* Time display red */
#define COLOR_WHITE        lv_color_hex(0xFFFFFF)     /* White text */
#define COLOR_YELLOW       lv_color_hex(0xFFA726)     /* Temperature yellow */

/* UI Objects */
static lv_obj_t *main_screen;
static lv_obj_t *time_label;
static lv_obj_t *date_label;
static lv_obj_t *evening_label;
static lv_obj_t *temp_label;
static lv_obj_t *day_label;
static lv_obj_t *humidity_label;

/* Timer for updating time */
static lv_timer_t *time_timer;

/* Month names array */
static const char* months[] = {
    "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
    "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
};

/* Day names array */
static const char* days[] = {
    "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"
};

/**
 * Update time display with real system time
 */
static void update_time_cb(lv_timer_t *timer)
{
    time_t now;
    struct tm *tm_info;
    char time_str[16];
    char date_str[32];
    
    time(&now);
    tm_info = localtime(&now);
    
    /* Format time as HH:MM:SS */
    sprintf(time_str, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
    lv_label_set_text(time_label, time_str);
    
    /* Format date as per image: 2024   DECEMBER   30 */
    sprintf(date_str, "%d   %s   %d", 
            1900 + tm_info->tm_year, 
            months[tm_info->tm_mon], 
            tm_info->tm_mday);
    lv_label_set_text(date_label, date_str);
    
    /* Update day of week */
    lv_label_set_text(day_label, days[tm_info->tm_wday]);
}

/**
 * Create gradient background with multiple color stops
 */
static void create_gradient_bg(lv_obj_t *parent)
{
    /* Remove padding and borders */
    lv_obj_set_style_pad_all(parent, 0, 0);
    lv_obj_set_style_border_width(parent, 0, 0);
    lv_obj_set_style_radius(parent, 0, 0);
    
    /* Create complex gradient background */
    lv_obj_set_style_bg_color(parent, COLOR_BG_MID2, 0);
    lv_obj_set_style_bg_grad_color(parent, COLOR_BG_TOP, 0);
    lv_obj_set_style_bg_grad_dir(parent, LV_GRAD_DIR_VER, 0);
    lv_obj_set_style_bg_opa(parent, LV_OPA_COVER, 0);
}

/**
 * Create top header area with enhanced styling
 */
static void create_header(lv_obj_t *parent)
{
    /* Header container */
    lv_obj_t *header = lv_obj_create(parent);
    lv_obj_set_size(header, lv_pct(100), 60);
    lv_obj_align(header, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_bg_color(header, COLOR_BG_MID1, 0);
    lv_obj_set_style_bg_opa(header, LV_OPA_90, 0);
    lv_obj_set_style_border_width(header, 0, 0);
    lv_obj_set_style_radius(header, 0, 0);
    lv_obj_set_style_pad_all(header, 0, 0);
    
    /* Date label with enhanced styling */
    date_label = lv_label_create(header);
    lv_label_set_text(date_label, "2024   DECEMBER   30");
    lv_obj_set_style_text_font(date_label, &lv_font_montserrat_18, 0);
    lv_obj_set_style_text_color(date_label, COLOR_WHITE, 0);
    lv_obj_set_style_text_letter_space(date_label, 2, 0);
    lv_obj_center(date_label);
}

/**
 * Create main time display area with perfect styling
 */
static void create_time_display(lv_obj_t *parent)
{
    /* Time container */
    lv_obj_t *time_container = lv_obj_create(parent);
    lv_obj_set_size(time_container, lv_pct(100), 220);
    lv_obj_align(time_container, LV_ALIGN_CENTER, 0, -10);
    lv_obj_set_style_bg_opa(time_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(time_container, 0, 0);
    lv_obj_set_style_pad_all(time_container, 20, 0);
    
    /* Evening section container */
    lv_obj_t *evening_container = lv_obj_create(time_container);
    lv_obj_set_size(evening_container, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_obj_align(evening_container, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_set_style_bg_opa(evening_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(evening_container, 0, 0);
    lv_obj_set_style_pad_all(evening_container, 0, 0);
    lv_obj_set_flex_flow(evening_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(evening_container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_gap(evening_container, 10, 0);
    
    /* Evening label */
    evening_label = lv_label_create(evening_container);
    lv_label_set_text(evening_label, "EVENING");
    lv_obj_set_style_text_font(evening_label, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(evening_label, COLOR_TIME_RED, 0);
    lv_obj_set_style_text_letter_space(evening_label, 1, 0);
    
    /* Clock icon using Unicode */
    lv_obj_t *clock_icon = lv_label_create(evening_container);
    lv_label_set_text(clock_icon, LV_SYMBOL_REFRESH);
    lv_obj_set_style_text_font(clock_icon, &lv_font_montserrat_20, 0);
    lv_obj_set_style_text_color(clock_icon, COLOR_TIME_RED, 0);
    
    /* Main time display with large font */
    time_label = lv_label_create(time_container);
    lv_label_set_text(time_label, "22:02:56");
    lv_obj_set_style_text_font(time_label, &lv_font_montserrat_48, 0);
    lv_obj_set_style_text_color(time_label, COLOR_TIME_RED, 0);
    lv_obj_set_style_text_letter_space(time_label, 2, 0);
    lv_obj_align(time_label, LV_ALIGN_CENTER, 0, 15);
    
    /* Add text shadow effect */
    lv_obj_set_style_shadow_width(time_label, 4, 0);
    lv_obj_set_style_shadow_color(time_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_shadow_opa(time_label, LV_OPA_20, 0);
    lv_obj_set_style_shadow_offset_x(time_label, 0, 0);
    lv_obj_set_style_shadow_offset_y(time_label, 2, 0);
}

/**
 * Create enhanced bottom status bar
 */
static void create_status_bar(lv_obj_t *parent)
{
    /* Status bar container with enhanced background */
    lv_obj_t *status_bar = lv_obj_create(parent);
    lv_obj_set_size(status_bar, lv_pct(100), 80);
    lv_obj_align(status_bar, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_color(status_bar, COLOR_BG_BOTTOM, 0);
    lv_obj_set_style_bg_opa(status_bar, LV_OPA_90, 0);
    lv_obj_set_style_border_width(status_bar, 0, 0);
    lv_obj_set_style_radius(status_bar, 0, 0);
    lv_obj_set_style_pad_all(status_bar, 0, 0);
    lv_obj_set_style_pad_left(status_bar, 20, 0);
    lv_obj_set_style_pad_right(status_bar, 20, 0);
    
    /* Temperature section */
    lv_obj_t *temp_container = lv_obj_create(status_bar);
    lv_obj_set_size(temp_container, 80, lv_pct(100));
    lv_obj_align(temp_container, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_bg_opa(temp_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(temp_container, 0, 0);
    lv_obj_set_style_pad_all(temp_container, 0, 0);
    lv_obj_set_flex_flow(temp_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(temp_container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_gap(temp_container, 4, 0);
    
    /* Temperature value */
    temp_label = lv_label_create(temp_container);
    lv_label_set_text(temp_label, "25.6");
    lv_obj_set_style_text_font(temp_label, &lv_font_montserrat_24, 0);
    lv_obj_set_style_text_color(temp_label, COLOR_YELLOW, 0);
    
    /* Temperature icon */
    lv_obj_t *temp_icon = lv_label_create(temp_container);
    lv_label_set_text(temp_icon, LV_SYMBOL_BATTERY_FULL);  /* Using battery as thermometer substitute */
    lv_obj_set_style_text_font(temp_icon, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(temp_icon, COLOR_YELLOW, 0);
    
    /* Day of week - centered */
    day_label = lv_label_create(status_bar);
    lv_label_set_text(day_label, "MONDAY");
    lv_obj_set_style_text_font(day_label, &lv_font_montserrat_18, 0);
    lv_obj_set_style_text_color(day_label, COLOR_WHITE, 0);
    lv_obj_set_style_text_letter_space(day_label, 1, 0);
    lv_obj_align(day_label, LV_ALIGN_CENTER, 0, 0);
    
    /* Humidity section */
    lv_obj_t *humidity_container = lv_obj_create(status_bar);
    lv_obj_set_size(humidity_container, 100, lv_pct(100));
    lv_obj_align(humidity_container, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_set_style_bg_opa(humidity_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(humidity_container, 0, 0);
    lv_obj_set_style_pad_all(humidity_container, 0, 0);
    lv_obj_set_flex_flow(humidity_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(humidity_container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_gap(humidity_container, 2, 0);
    
    /* Humidity icon at top right */
    lv_obj_t *humidity_icon = lv_label_create(humidity_container);
    lv_label_set_text(humidity_icon, LV_SYMBOL_WIFI);
    lv_obj_set_style_text_font(humidity_icon, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(humidity_icon, COLOR_WHITE, 0);
    lv_obj_align(humidity_icon, LV_ALIGN_TOP_RIGHT, -5, 5);
    
    /* Humidity text */
    lv_obj_t *humidity_text = lv_label_create(humidity_container);
    lv_label_set_text(humidity_text, "HUMIDITY");
    lv_obj_set_style_text_font(humidity_text, &lv_font_montserrat_12, 0);
    lv_obj_set_style_text_color(humidity_text, COLOR_WHITE, 0);
    lv_obj_set_style_text_letter_space(humidity_text, 0, 0);
    
    /* Humidity value */
    humidity_label = lv_label_create(humidity_container);
    lv_label_set_text(humidity_label, "65");
    lv_obj_set_style_text_font(humidity_label, &lv_font_montserrat_18, 0);
    lv_obj_set_style_text_color(humidity_label, COLOR_WHITE, 0);
}

/**
 * Initialize and show the enhanced clock UI
 */
void clock_ui_show(void)
{
    /* Create main screen */
    main_screen = lv_obj_create(NULL);
    lv_obj_set_size(main_screen, lv_pct(100), lv_pct(100));
    
    /* Create gradient background */
    create_gradient_bg(main_screen);
    
    /* Create UI components */
    create_header(main_screen);
    create_time_display(main_screen);
    create_status_bar(main_screen);
    
    /* Load the screen */
    lv_screen_load(main_screen);
    
    /* Start time update timer */
    time_timer = lv_timer_create(update_time_cb, 1000, NULL);
    
    /* Initial time update */
    update_time_cb(NULL);
}

/**
 * Clean up resources
 */
void clock_ui_cleanup(void)
{
    if (time_timer) {
        lv_timer_delete(time_timer);
        time_timer = NULL;
    }
} 